/* 
Scenic Graphics Library
Copyright (C) 2007 Jouni Tulkki

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USApackage scenic;
*/

package scenic.toolkit.text;

import java.awt.Font;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import java.awt.geom.Rectangle2D;
import java.util.Vector;

import scenic.Color;
import scenic.Graphics;
import scenic.SceneContainer;
import scenic.toolkit.CComponent;


public class TextElement extends Element {
	String string;
	TextStyle style;
	
	public TextElement(String string, TextStyle style) {
		this.string = string;
		this.style = style;
	}

	public int getNumItems() {
		return string.length();
	}
	
	public ElementItem getItemInfo(int index, ElementItem info) {
		if(info == null)
			info = new ElementItem();
		
		FontRenderContext frc = new FontRenderContext(
				paragraph.getTotalTransform(),
				true, false);
		Font font = style.getFont();
		GlyphVector glyphVector = font.createGlyphVector(frc, string.substring(index, index + 1));
		Rectangle2D bounds = glyphVector.getLogicalBounds();
		
		info.element = this;
		info.width = bounds.getWidth();
		info.ascent = -bounds.getY();
		info.descent = bounds.getMaxY();
		info.isSpace = Character.isSpaceChar(string.charAt(index));
		info.index = index;
		
		return info;
	}
	
	
	public void drawObjects(SceneContainer scene, Vector<ElementItem> objects) {
		Graphics g = new Graphics(scene);
		float positions[] = new float[objects.size() * 2];
		
		for(int i = 0; i < objects.size(); i++) {
			positions[i * 2] = (float)objects.elementAt(i).x;
			positions[i * 2 + 1] = (float)objects.elementAt(i).y;
		}
		
		g.setFont(style.getFont());
		g.setFillColor(new Color(0.0f));
		g.drawText(string.substring(objects.elementAt(0).index, objects.elementAt(objects.size() - 1).index + 1), positions);
	}
}
